package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.DmcConstant;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.button.BindCsunButtonLocationDto;
import com.csun.cmny.provider.model.dto.button.CsunButtonPageQuery;
import com.csun.cmny.provider.model.dto.csundoor.BindCsunDoorLocationDto;
import com.csun.cmny.provider.model.dto.csundoor.CsunDoorPageQuery;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.dto.fallradar.BindFallRadarLocationDto;
import com.csun.cmny.provider.model.dto.fallradar.FallRadarPageQuery;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.csunbutton.DmcCsunButtonVo;
import com.csun.cmny.provider.model.vo.csundoor.DmcCsunDoorVo;
import com.csun.cmny.provider.model.vo.fallradar.DmcFallRadarVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcCsunButtonLocationService;
import com.csun.cmny.provider.service.DmcCsunDoorLocationService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcFallRadarLocationService;
import com.csun.cmny.provider.validator.DmcDeviceEditValidator;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 跌倒雷达
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-09-02 14:05
 * @Description: the class dmc csun camera controller
 */
@Slf4j
@RestController
@RequestMapping(value = "/fallRadar", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcFallRadarController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private DmcFallRadarLocationService dmcFallRadarLocationService;

    /**
     * 查询雷达设备列表
     * @return  the wrapper
     */
    @RequestMapping(value = "/queryRadarListWithPage", method = RequestMethod.POST)
    public Wrapper queryRadarListWithPage(@RequestBody FallRadarPageQuery fallRadarPageQuery) {
        fallRadarPageQuery.setGroupId(getLoginAuthDto().getGroupId());
        fallRadarPageQuery.setDeviceType(DmcDeviceTypeEnum.FALL_RADAR.getIndex());
        PageInfo pageInfo = dmcFallRadarLocationService
                .queryGroupFallRadarListWithPage(fallRadarPageQuery);
        for (Object o : pageInfo.getList()) {
            DmcFallRadarVo dmcFallRadarVo = (DmcFallRadarVo) o;
            if (!PubUtils.isNull(dmcFallRadarVo.getBindType())) {
                dmcFallRadarVo.setBind(true);
            }
        }
        // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * 添加fall radar
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addFallRadar(@Validated @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {
        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.FALL_RADAR.getIndex());
        Integer result = dmcDeviceService.addDevice(dmcDevice, getLoginAuthDto());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 修改fall radar
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper editFallRadar(@Validated(value = {DmcDeviceEditValidator.class})
                                @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.FALL_RADAR.getIndex());
        Integer result = dmcDeviceService.editDevice(dmcDevice, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            // 删除redisKey
        }
        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 根据Id删除fall radar
     *
     * @param id    the id
     *
     * @return  the wrapper
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteFallRadarById(@PathVariable Long id) {

        Integer result = dmcFallRadarLocationService.deleteFallRadarById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            // 删除redisKey
        }
        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 绑定fall radar
     *
     * @param bindFallRadarLocationDto bind fall radar location dto
     *
     * @return the wrapper.
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/bindFallRadarLocation", method = RequestMethod.POST)
    public Wrapper bindFallRadarLocation(@Validated @RequestBody BindFallRadarLocationDto bindFallRadarLocationDto,
                                        BindingResult br) {
        Integer result = dmcFallRadarLocationService.bindFallRadarLocation(bindFallRadarLocationDto,
                getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            // 添加redisKey
        }
        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 解绑紧急按钮
     *
     * @param deviceId  device id
     *
     * @return  the wrapper.
     */
    // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/unBindFallRadarLocation/{deviceId}", method = RequestMethod.POST)
    public Wrapper unBindFallRadarLocation(@PathVariable Long deviceId) {

        Integer result = dmcFallRadarLocationService.breakFallRadarLocation(deviceId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            // 删除redisKey
        }
        //  return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
}
